package com.juc.demo;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author yuanzheng
 * @date 2020/8/7-17:31
 */
public class CallableTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        new Thread().start();
        MyThread myThread = new MyThread();
        FutureTask futureTask = new FutureTask(myThread);
        // 适配类
        new Thread(futureTask, "A").start();
        new Thread(futureTask, "B").start();
        // 结果会被缓存，效率高
        Integer o = (Integer) futureTask.get();
        //这个get 方法可能会产生阻塞
        System.out.println(o);
    }
}

class MyThread implements Callable<Integer> {
    @Override
    public Integer call() {
        System.out.println("call()");
        return 1024;
    }
}
